//处理提交表单返回的数据
var sendMobileSuccessCallback = function(data) {
    formInlineRight($('#verifyVcodeTrigger'));
    timeDisplay($('#verifyVcodeTrigger'), 60);
};
var sendNewMobileSuccessCallback = function(data) {
    formInlineRight($('#newVcodeTrigger'));
    timeDisplay($('#newVcodeTrigger'), 60);
};
var sendMobileErrorCallback = function(data) {
    // $(this).removeAttr('disabled');
    if (typeof data.responseJSON == 'undefined') {
        formInlineTip($('#submitVerifyMobile'), lang('tip.networkPoor'), 0);
    } else {
        formInlineTip($('#submitVerifyMobile'), data.responseJSON.info, 0);
    }
};
var verifyWithMobileSuccessCallback = function(data) {
    formInlineRight($('#submitVerifyMobile'));
    $('#verifyMobileForm').hide();
    $('#changeMobileForm').removeClass('hidden');
    $('#submitVerifyMobile').addClass('hidden');
    $('#submitChangeMobile').removeClass('hidden'); //  $("#submitVerifyMobile").attr("disabled", false);
};
var verifyWithMobileErrorCallback = function(data) {
    //  $("#submitVerifyMobile").attr("disabled", false);
    if (typeof data.responseJSON == 'undefined') {
        formInlineTip($('#submitVerifyMobile'), lang('tip.networkPoor'), 0);
    } else {
        formInlineTip($('#submitVerifyMobile'), data.responseJSON.info, 0);
    }
};
var loadAuthoritySuccessCallback = function(data) {
    //清除选中数据
    $('#authorityModal').find('input[type="checkbox"]').prop('checked', false);
    var authorityData = data.data;
    for (var i = 0; i < authorityData.length; i++) {
        for (var j = 0; j < authorityData[i].authoritys.length; j++) {
            $('input[type=checkbox].hotel[value=' + authorityData[i].hotelId + ']').prop('checked', true);
            $('#hotel_' + authorityData[i].hotelId).find('.authority[value=' + authorityData[i].authoritys[j].hotelauthority_id + ']').prop('checked', true);
        }
    }
};
var loadAuthorityErrorCallback = function(data) {};
var checkUserSuccessCallback = function(data) {
    $('#addName').val(data.data.name);
    $('#addName').attr('disabled', 'disabled');
};
var checkUserErrorCallback = function(data) {
    $('#addName').val('');
    $('#addName').removeAttr('disabled');
};
$(function() {
    //编辑主账户
    $('#infoModal').on('show.bs.modal', function(e) {
        var triggerId = $(e.relatedTarget);
        $('#editName').val(triggerId.data('name'));
        $('#submitInfo').attr('data-value', triggerId.data('id'));
    });
    //修改信息对话框
    $('#infoModal').inputValidate({
        submit: {
            query: '#submitInfo'
        },
        inputs: [{
            query: '#editName',
            regexp: window.commonRegexp.nameLength20,
            message: lang('tip.name'),
            emptyMessage: lang('tip.nameNull')
        }]
    });
    $('#submitInfo').on('successSubmit.amos.inputValidate', function(e) {
        var submitBtn = $(this);
        var params = {};
        params.name = $('#editName').val();
        params.head = $('#uploadPicture').val();
        ajaxRequest('/Home/Dealer/updateUserInfo', params, 'post', null, null, submitBtn);
    });
    //更换密码
    $('#changeMobileModal').on('show.bs.modal', function(e) {
        var triggerTd = $(e.relatedTarget);
        formInlineRight($('#verifyMobileCode'));
        formInlineRight($('#submitVerifyMobile'));
        formInlineRight($('#newMobile'));
        formInlineRight($('#newMobileCode'));
        formInlineRight($('#submitChangeMobile'));
        $('#verifyMobile').val(triggerTd.data('mobile'));
    });
    $('#changePasswordModal').inputValidate({
        submit: {
            query: '#submitChangePassword'
        },
        inputs: [{
            query: '#requestPassword',
            regexp: window.commonRegexp.password,
            message: lang('tip.password'),
            emptyMessage: lang('tip.passwordNull')
        }, {
            query: '#requestNewPassword',
            regexp: window.commonRegexp.password,
            message: lang('tip.password'),
            emptyMessage: lang('tip.passwordNull')
        }, {
            query: '#requestConfirmNew',
            regexp: window.commonRegexp.password,
            message: lang('tip.password'),
            emptyMessage: lang('tip.passwordNull')
        }, {
            query: '#requestVcode',
            regexp: window.commonRegexp.vcode,
            message: lang('tip.verification'),
            emptyMessage: lang('tip.verificationNull')
        }]
    });
    $('#submitChangePassword').on('successSubmit.amos.inputValidate', function(e) {
        var submitBtn = $(this);
        //检查两次输入密码是否正确
        if ($('#requestConfirmNew').val() != $('#requestNewPassword').val()) {
            formInlineTip($('#submitChangePassword'), lang('dealer.wrongAgain'));
            return false;
        }
        var params = {};
        params.oldPassword = $('#requestPassword').val();
        params.newPassword = $('#requestConfirmNew').val();
        params.vcode = $('#requestVcode').val();
        ajaxRequest('/Home/Dealer/updatePassword', params, 'post', null, function(error) {
            if (error.responseJSON === undefined) {
                formInlineTip(submitBtn, lang('tip.server'));
            } else {
                formInlineTip(submitBtn, error.responseJSON.info);
            }
            refreshVcode();
            submitBtn.removeAttr('disabled');
        }, submitBtn);
    });
    //更换手机号
    //changeMobileModal
    $('#changeMobileModal').inputValidate({
        submit: {
            query: '#submitVerifyMobile'
        },
        inputs: [{
            query: '#verifyMobileCode',
            regexp: window.commonRegexp.numbervcode6,
            message: lang('tip.verification6'),
            emptyMessage: lang('tip.messages')
        }]
    });

    $('#verifyVcodeTrigger').on('click', function(e) {
        e.preventDefault();
        if ($(this).attr('disabled') !== undefined) {
            return false;
        }
        //  $(this).attr('disabled', true);
        var params = {};
        ajaxRequest('/Home/Dealer/sendMobile', params, 'post', sendMobileSuccessCallback, sendMobileErrorCallback, $(this));
    });
    $('#submitVerifyMobile').on('successSubmit.amos.inputValidate', function(e) {
        e.preventDefault();
        if ($(this).attr('disabled') !== undefined) {
            return false;
        }
        // $(this).attr('disabled', true);
        var params = {};
        params.vcode = $('#verifyMobileCode').val();
        ajaxRequest('/Home/Dealer/verifyWithMobile', params, 'post', verifyWithMobileSuccessCallback, verifyWithMobileErrorCallback);
    });
    $('#changeMobileForm').inputValidate({
        submit: {
            query: '#newVcodeTrigger'
        },
        inputs: [{
            query: '#newMobile',
            regexp: window.commonRegexp.mobile,
            message: lang('tip.mobile'),
            emptyMessage: lang('tip.mobileNull')
        }]
    });
    // 新手机号码发送短信验证
    $('#newVcodeTrigger').on('successSubmit.amos.inputValidate', function(e) {
        e.preventDefault();
        if ($(this).attr('disabled') !== undefined) {
            return false;
        }
        //  $(this).attr('disabled', true);
        var params = {};
        params.mobile = $('#newMobile').val();
        params.areaCode = $('#changeMobileForm .areaCodeValue').text().trim();
        ajaxRequest('/Home/Dealer/sendMobileNew', params, 'post', sendNewMobileSuccessCallback, null, $(this));
    });
    $('#changeMobileModalDialog').inputValidate({
        submit: {
            query: '#submitChangeMobile'
        },
        inputs: [{
            query: '#newMobile',
            regexp: window.commonRegexp.mobile,
            message: lang('tip.mobile'),
            emptyMessage: lang('tip.mobileNull')
        }, {
            query: '#newMobileCode',
            regexp: window.commonRegexp.numbervcode6,
            message: lang('tip.verification6'),
            emptyMessage: lang('tip.messages')
        }]
    });
    //验证新的手机号
    $('#submitChangeMobile').on('successSubmit.amos.inputValidate', function(e) {
        e.preventDefault();
        if ($(this).attr('disabled') !== undefined) {
            return false;
        }
        // $(this).attr('disabled', true);
        var params = {};
        params.vcode = $('#newMobileCode').val();
        params.mobile = $('#newMobile').val();
        params.areaCode = $('#changeMobileForm .areaCodeValue').text().trim();
        ajaxRequest('/Home/Dealer/verifyWithMobileNew', params, 'post', null, null, $(this));
    });

    $('#showOrganizationNoContainer').fileUpload({
        url: '/Home/Dealer/upload',
        urlType: 'POST',
        autoUpload: true,
        success: function(data, trigger, result) {
            if (data.status == 200) {
                $('#uploadPicture').val(data.data.filename);
                result.attr('src', data.data.filename);
                formInlineTip(trigger, lang('dealer.uploadSucceed'), 2);
            }
        },
        failure: function(textStatus, trigger) {
            formInlineTip(trigger, lang('photograph.6'));
        }
    });
    // 自动上传，配置参数
    $('#doc-info-form .fileUpload').fileUpload('changeUploadParam', {
        'config': 'user'
    });

    $('#addMobile').blur(function() {
        $('#ajaxSeachLoad').html('');
        var params = {};
        params.mobile = $(this).val();
        params.areaCode = $('#addModal .areaCodeValue').text().trim();
        ajaxRequest('/Home/Account/checkUserExist', params, 'post', checkUserSuccessCallback, checkUserErrorCallback);
    });

    $('#popularizeModal').on('show.bs.modal', function(e) {
        formInlineRight($('#popularizeName'));
        formInlineRight($('#submitPopularizeInfo'));
    });
    $('#popularizeModal').on('successSubmit.amos.inputValidate', function(e) {
        var submitBtn = $('#submitPopularizeInfo');
        var params = {};
        params.name = $('#popularizeName').val();
        params.imageUrl = submitBtn.data('image');
        ajaxRequest('/Home/Account/savePublicLinkInfo', params, 'post', function() {
            formInlineTip(submitBtn, lang('successfullySet'), 1);
        }, null, submitBtn);
    });

    $('#popularizeModal .picuture-upload, #popularizeModal .picture-re-upload').on('click', function(e) {
        $('#submitPicture').data('config', 'publicLink').data('type', window.accountConstant.popularize);
        $('#pictureSelector').trigger('click');
    });
    window.accountConstant = { 'popularize': 1 };
 
});
var refreshPopularizeImage = function(url) {
    $('#popularizeModal .picture').css('background-image', 'url(' + url + ')').removeClass('hidden');
    $('#submitPopularizeInfo').data('image', url);
};
var pictureCropperSuccess = function(imageUrl, imageType) {
    switch (imageType) {
        case window.accountConstant.popularize:
            refreshPopularizeImage(imageUrl);
            $('#pictureModal').modal('hide');
            break;
        default:
            break;
    }
};
